from odoo import models, api


class TransferIssueReport(models.AbstractModel):
    _name = "report.sb_workflow_purchase.purchase_po_outsource_label"
    _description = '外发采购单'

    @api.model
    def _get_report_values(self, docids, data=None):
        purchase_ids = self.env['purchase.order'].search([('id', 'in', docids)])
        data = []
        for purchase in purchase_ids:
            order_line = []
            tax_totals = purchase.tax_totals
            taxs = []
            for k, tax in tax_totals.get('groups_by_subtotal').items():
                for rec in tax:
                    taxs.append({
                        'name': rec.get('tax_group_name'),
                        'amount': rec.get('formatted_tax_group_amount')
                    })
            for line in purchase.order_line:

                order_line.append({
                    'id': line.id,
                    'work': line.s_production_id.name,
                    'product_id': line.s_subcontracting_product_id.name,  # 型号
                    'area_price_unit': line.area_price_unit,
                    'price_unit': line.price_unit,
                    'price_subtotal': line.price_subtotal,
                    's_service_pricing_method': line.product_id.s_service_pricing_method,
                    's_area': line.s_area,
                    'area_pnl_qty': line.area_pnl_qty,  # m²/PNL
                    's_specs': line.s_specs,  # 规格 pnll
                    'product_uom': 'PNL',  # 计量单位
                    'product_qty': line.product_qty,  # 数量
                    'remark': line.name  # 备注
                })
            dtl = {
                'id': purchase.id,
                'name': purchase.name,
                'date_approve': purchase.date_approve,
                'type': purchase.order_line[0].product_id.name if purchase.order_line else '',
                'partner_id': purchase.partner_id.name,
                'order_line': order_line,
                'amount_untaxed': tax_totals.get('formatted_amount_untaxed'),
                'amount_total': tax_totals.get('formatted_amount_total'),
                'taxs': taxs
            }
            data.append(dtl)
        return {
            'report_title': 'Purchase Order(PDF)',
            'doc_model': 'purchase.order',
            'docs': docids,
            'data': data,
        }
